#pragma once

#include "type.h"

#include "chunk.h"
#include "IRenderer.h"

class D3DRenderer;

class ChunkDebugMesh
	: public ChunkSystem::IRenderable, public ChunkSystem::ChunkItem::LoadProgressListener
{
public:
	ChunkDebugMesh(ChunkSystem::Chunk* pChunk);
	virtual ~ChunkDebugMesh(void);

	//IRenderable
	virtual void Load( ChunkSystem::IRenderer* pRenderer );
	virtual void Unload( void );
	virtual void Draw( ChunkSystem::IRenderer* pRenderer );

	//loading listener
	virtual void OnProgressIntervalNotify( uint16 uPercentage );

private:

	void _DrawMeshAABBWire( LPDIRECT3DDEVICE9 pDevice );
	void _DrawMeshHullWire( LPDIRECT3DDEVICE9 pDevice );

	bool _BuildMeshAABBWire( LPDIRECT3DDEVICE9 pDevice );
	bool _BuildMeshHullWire( LPDIRECT3DDEVICE9 pDevice );

private:

	bool					m_bBuiltFlag;
	ChunkSystem::Chunk*		m_pChunk;

	bool					m_bShowAABB;
	bool					m_bShowHull;

	LPDIRECT3DVERTEXBUFFER9 m_pAABBVB;
	LPDIRECT3DINDEXBUFFER9  m_pAABBIB;

	LPDIRECT3DVERTEXBUFFER9 m_pHullVB;
	LPDIRECT3DINDEXBUFFER9  m_pHullIB;
	int						m_iHullLineCount;

	struct PortalMesh
	{
		LPDIRECT3DVERTEXBUFFER9 m_pPortalVB;
		int m_VertexCount;
	};
	typedef std::vector< PortalMesh > PortalMeshes;
	typedef PortalMeshes::iterator itrPortalMesh;

	PortalMeshes			m_conPortalMeshes;

	CRITICAL_SECTION m_CriticalSection;
};
